package com.kgd.news.controller.admin;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.upload.UploadFile;
import com.kgd.news.entity.Media;
import com.kgd.news.entity.ServiceMessage;
import com.kgd.news.entity.Smallapp;
import com.kgd.news.utils.Api;
import com.kgd.news.utils.BaseController;
import com.kgd.news.utils.StringUtils;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @Email: huangsanm@foxmail.com
 * @Date: 2017/10/9
 * @Time: 下午3:03
 */
public class ServiceMessageController extends BaseController {

    @Override
    public void list() {
        String sql = "from service_message where 1=1 ";
        String type = "service_message";
        Page<ServiceMessage> data = ServiceMessage.dao.paginate(getPageIndex(type), PAGE_SIZE, "select *", sql);
        setAttr("data", data);
        render("servicemessage_list.html");
    }

    @Override
    public void edit() {
        Integer id = getParaToInt("id");
        ServiceMessage img = ServiceMessage.dao.findById(id);
        setAttr("servicemessage", img);
        render("servicemessage_edit.html");
    }

    @Override
    public void save() {
         getFile("cover");
        ServiceMessage us = getModel(ServiceMessage.class, "servicemessage");
        String isChoose = us.getIschoose();
        if ("YES".equals(isChoose)) {
            List<ServiceMessage> serviceMessageList = ServiceMessage.dao.find("select * from service_message where ischoose = 'YES' ");
            for (ServiceMessage serviceMessage : serviceMessageList
                    ) {
                    serviceMessage.setIschoose("NO");
                    serviceMessage.update();
            }
        }
        if (us != null) {
            if (us != null && us.getId() != null) {
                us.update();
            } else if (us != null && us.getId() == null) {
                us.save();
            }
            CacheKit.removeAll("service_message");
        }
        list();
    }

    public void alert(String message) {
        try {
            PrintWriter out = getResponse().getWriter();
            out.print("<script>alert('say what you want to say');</script>");
            out.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void del() {
        String ids = getPara("ids");
        Db.update("delete from service_message where id in (" + ids + ")");
        CacheKit.removeAll("service_message");
        list();
    }


}
